<?php

/**
 *TrainerCity 商品类型管理程序 *
 
 * $Id: goods_type.php 17063 2010-03-25 06:35:46Z  $
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . '/' . ADMIN_PATH . '/includes/lib_goods.php');
include_once(ROOT_PATH . '/includes/cls_image.php');
include_once(ROOT_PATH . '/includes/lib_main.php');

$exc = new exchange($ecs->table("goods_type"), $db, 'cat_id', 'cat_name');

/*------------------------------------------------------ */
//-- Manage UI
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'manage')
{
    #assign_query_info();

    #$smarty->assign('ur_here',          $_LANG['08_goods_type']);
    $smarty->assign('full_page',        1);

	#add by wanglei
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('state');
	$state=$db->getAll($sql);
	
	$taskcount=count($state);
		
	$record_count = $taskcount;
	$page = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1;
	$pagesize = 10;
	$pager = get_pager('state.php', array('act' => 'manage'), $record_count, $page, $pagesize);
	$fitness_lists = get_msg_list($pagesize, $pager['start']);
	$smarty->assign('pager', $pager);
	 
	$smarty->assign('state',   $fitness_lists);
	
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('country');
	$country=$db->getAll($sql);
	
	$smarty->assign('country',   $country);
	
	#end by wanglei
	$smarty->assign('action_link',      array('text' => 'Add New Type', 'href' => 'state.php?act=add'));

    $smarty->display('state.htm');
}

/*------------------------------------------------------ */
//-- Add New Type
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'add_newtype')
{
	 $new_type = !empty($_POST['new_type']) ? trim($_POST['new_type']) : '';
	 $country = !empty($_POST['country']) ? intval($_POST['country']) : '';
	 
	if($new_type)
	{
		/*Insert*/	
		$sql = "INSERT INTO " . $ecs->table('state') . "(country_id, state_name) VALUES ('" . $country . "', '" . $new_type . "')";
    	if($db->query($sql) != false)
		{
			/*Add Successfully*/	
			$links = array(array('href' => 'state.php?act=manage', 'text' => "State List"));
        	sys_msg("New Type Add Successfully!", 0, $links);
		}
		else
		{
			/*Add Failed*/	
			$links = array(array('href' => 'state.php?act=manage', 'text' => "State List"));
        	sys_msg("Failed, please try again!", 0, $links);
		}
	}
	else
	{
		/*No new type*/	
			$links = array(array('href' => 'state.php?act=manage', 'text' => "State List"));
        	sys_msg("Please input new type!", 0, $links);
	}
}

/*------------------------------------------------------ */
//-- Modify Type
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'modify')
{
	$cat_id = !empty($_GET['cat_id']) ? intval($_GET['cat_id']) : '';
	
	#add by wanglei
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('state')." WHERE state_id='$cat_id' ";
	$state=$db->getRow($sql);
	
	$smarty->assign('state',   $state);
	
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('country');
	$country=$db->getAll($sql);
	
	$smarty->assign('country',   $country);
	
	#end by wanglei
	$smarty->assign('action_link',      array('text' => 'Add New Type', 'href' => 'state.php?act=add'));

    $smarty->display('state_add.htm');
}

/*------------------------------------------------------ */
//-- Modify Type
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'act_modify')
{
	$cat_id = !empty($_POST['sessions_type_num']) ? intval($_POST['sessions_type_num']) : '';
	$country = !empty($_POST['country']) ? intval($_POST['country']) : '';
	$sessions_type = !empty($_POST['sessions_type']) ? trim($_POST['sessions_type']) : '';
	
	if($cat_id && $sessions_type)
	{
		#add by wanglei
		$sql = "UPDATE " . $GLOBALS['ecs']->table('state') .
            " SET state_id='$cat_id', country_id = '$country' , state_name='$sessions_type' WHERE state_id='$cat_id' ";
		#$GLOBALS['db']->query($sql);
		$db->query($sql);
		if($db->query($sql) != false)
		{
			/*Modify Successfully*/	
			$links = array(array('href' => 'state.php?act=manage', 'text' => "State List"));
        	sys_msg("Modify Type Successfully!", 0, $links);
			
		}
		else
		{
			/*Modify Failed*/	
			$links = array(array('href' => 'state.php?act=manage', 'text' => "State List"));
        	sys_msg("Failed, please try again!", 0, $links);
		}
	}
	else
	{
			/*Modify Failed*/	
			$links = array(array('href' => 'state.php?act=manage', 'text' => "State List"));
        	sys_msg("Please input the num and tyep!", 0, $links);
	}
	
}

/*------------------------------------------------------ */
//-- Modify Type
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'delete')
{
	$cat_id = !empty($_GET['cat_id']) ? intval($_GET['cat_id']) : '';
		
	#add by wanglei
	$sql = "DELETE FROM " . $GLOBALS['ecs']->table('state') .
            " WHERE state_id='$cat_id' ";
	#$GLOBALS['db']->query($sql);

	$db->query($sql);
	if($db->query($sql) != false)
	{
		/*Modify Successfully*/	
		$links = array(array('href' => 'state.php?act=manage', 'text' => "State List"));
        sys_msg("Delete Type Successfully!", 0, $links);
			
	}
	else
	{
		/*Modify Failed*/	
		$links = array(array('href' => 'state.php?act=manage', 'text' => "State List"));
        sys_msg("Failed, please try again!", 0, $links);
	}	
}

/**
 * 
 *
 * @param   integer $num
 * @param   integer $start
 *
 * @return  array
 */
function get_msg_list($num, $start, $temp='', $sort='state_id')
{
    /*   */	
	$sql = 'SELECT * ' .'FROM ' . $GLOBALS['ecs']->table('state'). " AS s LEFT JOIN ". $GLOBALS['ecs']->table('country'). " AS c ON s.country_id = c.country_id " ;
	$sql = $sql." ORDER by ". $sort." ASC LIMIT ".$start.",".$num;
	$res = $GLOBALS['db']->getAll($sql);
	
	
    return $res;
}

?>
